﻿using ASP.NET_MVC5_Bootstrap3_3_1_LESS.Controllers;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Reflection;
using System.Web;
using System.Web.Mvc;

namespace WebApp
{
    public class NavModel
    {
        public string mi { get; set; }
        public string tab { get; set; }
        public string dd { get; set; }
        public string dd2 { get; set; }
        public string dd3 { get; set; }     
    }
    
    public class SiteStructure
    {
        string wContentPath = null;
        public SiteStructure(string wPath)
        {            
            wContentPath = wPath;

            foreach (string wFile in Directory.GetFiles(wPath, "home-*.html"))
            {
                HomeContent = File.ReadAllText(wFile);
                SiteName = Path.GetFileNameWithoutExtension(wFile).Substring(5);
                SiteStructure.UpdateLastUpdated(wFile);
                break;
            }

            MenuItems = new List<MenuItem>();
            foreach (string wDir in Directory.GetDirectories(wPath, "mi-*"))
            {
                SiteStructure.UpdateLastUpdated(wDir);
                MenuItems.Add(new MenuItem(wDir));
            }
            MenuItems.Sort();
        }

        public static void UpdateLastUpdated(string wFile)
        {
            DateTime wDt = File.GetLastWriteTime(wFile);
            if (wDt.CompareTo(SiteStructure.LastUpdated) > 0)
            {
                SiteStructure.LastUpdated = wDt;
            }
        }
        public List<MenuItem> MenuItems { get; set; }
        public string HomeContent { get; set; }
        public string SiteName { get; set; }

        public MenuItem this[string aMenuName]
        {
            get
            {
                foreach (MenuItem mi in MenuItems)
                {
                    if (mi.Name.Equals(aMenuName))
                    {
                        return mi;
                    }
                }
                return null;
            }
        }

        public static SiteStructure Instance { get; set; }
        public static DateTime LastUpdated = DateTime.MinValue;
        public static String LastUpdatedStr
        {
            get
            {
                return LastUpdated.ToShortDateString() + "  " + LastUpdated.ToShortTimeString();
            }
        }

        internal void Update()
        {
            if (HomeController.changed)
            {
                HomeController.Sync(wContentPath);
                HomeController.changed = false;                
            }
        }

        public static string mRoot = (System.Configuration.ConfigurationManager.AppSettings["ShareLocation"] != null ? System.Configuration.ConfigurationManager.AppSettings["ShareLocation"] : "");
        internal static string GetRoot()
        {
            if (!Directory.Exists(mRoot))
            {
                throw new System.IO.DirectoryNotFoundException("IIS was unable to access: " + mRoot);
            }
            return mRoot;
        }                   
    }

    public class MenuItem : IComparable
    {
        public MenuItem(string wDir)
        {
            wName = Path.GetFileNameWithoutExtension(wDir).Substring(3);
            Tabs = new List<TabItem>();
            foreach (string wPath in Directory.GetDirectories(wDir, "tab-*"))
            {
                SiteStructure.UpdateLastUpdated(wPath);
                Tabs.Add(new TabItem(wPath));
            }

            blnHasBeforeContent = false;
            blnHasAfterContent = false;
            if (File.Exists(wDir + "\\before.html"))
            {
                blnHasBeforeContent = true;
                SiteStructure.UpdateLastUpdated(wDir + "\\before.html");
                BeforeContent = File.ReadAllText(wDir + "\\before.html");
            }
            if (File.Exists(wDir + "\\after.html"))
            {
                blnHasAfterContent = true;
                SiteStructure.UpdateLastUpdated(wDir + "\\after.html");
                AfterContent = File.ReadAllText(wDir + "\\after.html");
            }

            Tabs.Sort();
        }

        public string BeforeContent { get; set; }
        public string AfterContent { get; set; }

        public string wName { get; set; }
        public string Name
        {
            get
            {
                int i = 0;
                string aName = wName;
                if (aName.Contains("-") && int.TryParse(aName.Substring(0, aName.IndexOf("-")), out i))
                {
                    aName = aName.Substring(aName.IndexOf("-") + 1);
                }
                return aName;
            }

        }

        public List<TabItem> Tabs { get; set; }
        public bool blnHasBeforeContent { get; set; }
        public bool blnHasAfterContent { get; set; }

        public TabItem this[string aMenuName]
        {
            get
            {
                foreach (TabItem mi in Tabs)
                {
                    if (mi.Name.Equals(aMenuName))
                    {
                        return mi;
                    }
                }
                return null;
            }
        }

        public int CompareTo(object obj)
        {
            return this.wName.CompareTo(((MenuItem)obj).wName);
        }
    }

    public class TabItem : IComparable
    {
        public TabItem(string wDir)
        {
            wName = Path.GetFileNameWithoutExtension(wDir).Substring(4);
            Dropdowns = new List<DropDownItem>();
            foreach (string wPath in Directory.GetFiles(wDir, "dd-*.html"))
            {
                SiteStructure.UpdateLastUpdated(wPath);
                Dropdowns.Add(new DropDownItem(wPath));
            }

            blnHasBeforeContent = false;
            blnHasAfterContent = false;
            if (File.Exists(wDir + "\\before.html"))
            {
                blnHasBeforeContent = true;
                SiteStructure.UpdateLastUpdated(wDir + "\\before.html");
                BeforeContent = File.ReadAllText(wDir + "\\before.html");
            }
            if (File.Exists(wDir + "\\after.html"))
            {
                blnHasAfterContent = true;
                SiteStructure.UpdateLastUpdated(wDir + "\\after.html");
                AfterContent = File.ReadAllText(wDir + "\\after.html");
            }
            Dropdowns.Sort();
        }

        public string BeforeContent { get; set; }
        public string AfterContent { get; set; }

        public string wName { get; set; }
        public string Name
        {
            get
            {
                int i = 0;
                if (wName.Contains("-") && int.TryParse(wName.Substring(0, wName.IndexOf("-")), out i))
                {
                    return wName.Substring(wName.IndexOf("-") + 1);
                }
                return wName;
            }

        }
        public List<DropDownItem> Dropdowns { get; set; }
        public bool blnHasBeforeContent { get; set; }
        public bool blnHasAfterContent { get; set; }

        public DropDownItem this[string aMenuName]
        {
            get
            {
                foreach (DropDownItem mi in Dropdowns)
                {
                    if (mi.Name.Equals(aMenuName))
                    {
                        return mi;
                    }
                }
                return null;
            }
        }

        public int CompareTo(object obj)
        {
            return this.wName.CompareTo(((TabItem)obj).wName);
        }

        public IEnumerable<SelectListItem> GetDropdownListItems(string mustContain)
        {
            if (mustContain == null) mustContain = "";
            List<SelectListItem> wRet = new List<SelectListItem>();
            List<string> items = new List<string>();
            SelectListItem item = new SelectListItem();
            item.Text = "";
            item.Value = "";
            wRet.Add(item);
            items.Add("");

            foreach (DropDownItem wdd in Dropdowns)
            {
                SelectListItem item2 = new SelectListItem();
                item2.Text = wdd.Name;
                item2.Value = wdd.Name;
                items.Add(wdd.Name);
                wRet.Add(item2);
            }

            if (!items.Contains(mustContain.Trim()))
            {
                SelectListItem item2 = new SelectListItem();
                item2.Text = mustContain;
                item2.Value = mustContain;
                items.Add(mustContain);
                wRet.Add(item2);
            }

            return wRet;
        }
    }

    public class DropDownItem : IComparable
    {
        public DropDownItem(string wPath)
        {
            wName = Path.GetFileNameWithoutExtension(wPath).Substring(3);
            SiteStructure.UpdateLastUpdated(wPath);
            Content = File.ReadAllText(wPath);
        }
        public string wName { get; set; }
        public string Name
        {
            get
            {
                int i = 0;
                if (wName.Contains("-") && int.TryParse(wName.Substring(0, wName.IndexOf("-")), out i))
                {
                    wName.Substring(wName.IndexOf("-") + 1);
                }
                return wName;
            }

        }
        public string Content { get; set; }

        public int CompareTo(object obj)
        {
            return this.wName.CompareTo(((DropDownItem)obj).wName);
        }
    }
}
